package com.jbm.jbmsupplier.net;

import android.os.Bundle;
import android.os.Handler;

import com.jbm.onehome.common.OHInterface;
import com.jbm.onehome.iface.GoodsClassEntity;
import com.jbm.onehome.iface.ICertAddress;
import com.jbm.onehome.iface.ICertAddressCtrl;
import com.jbm.onehome.iface.IExChangeCtrl;
import com.jbm.onehome.iface.IGeoinfoCtrl;
import com.jbm.onehome.iface.IOrder;
import com.jbm.onehome.iface.IOrderCtrl;
import com.jbm.onehome.iface.IorderEntity;
import com.jbm.onehome.iface.Media;

import java.io.InputStream;

/**
 * Created by 李征 on 2017/2/27.
 */

public class NetAddRess  {
    private  Handler handler;
    private Bundle mBundle;
    private Media m;
    private final String cmd_key="CMDKEY";
    private final String ADDRESS="ADDRESSSHOW";
    private final String ADDRESSMESSAGE="ADDRESSMESSAGE";
    private final String REMOVEAD="REMOVEAD";
    private final String GETALLORDER="GETALLORDER";
    private final String GETDQRORDER="GETDQRORDER";
    private final String GETZYZORDER="GETZYZORDER";
    private final String GETDPJORDER="GETDPJORDER";
    private final String ORDERITEM="ORDERITEM";
    private final String ORDERINPUT="ORDERINPUT";
    private final String ORDERTEMP="ORDERTEMP";
    private final String RATING="RATING";
    private final String REPAIS="REPAIS";
    private final String REPAISCG="REPAISCG";
    private final String MEDIAIPC="MEDIAIPC";
    private final String BAOXIU="BAOXIU";
    private final String YOUHUIQUAN="YOUHUIQUAN";
    private final String YOUHUIQUANDUIHUA="YOUHUIQUANDUIHUA";



    //-----------------------------------
    private  final String ADDRESSCENGJI="地址层级";
    public static final int ADDRESSCENGJIERROR= 0x111;

    private  final String DSYFIX="报修";
    public static final int DSYFIXERROR= 0x112;

    private  final String FIXCATE="报修二级分类";
    public static final int FIXCATEERROR= 0x113;





    public NetAddRess(Handler handler) {
        this.handler= handler;
    }


    protected void RunContext() {
        new Thread(new Runnable() {
            @Override
            public void run() {
        String cmd = mBundle.getString(cmd_key);
        if (cmd.equals(ADDRESS)){//获取地址信息
            ICertAddressCtrl addressCtrl;
            if(mBundle.getString("gi_type")==null){
                //dsy  代表获取全部地址
                 addressCtrl = OHInterface.getG_server().GetMyCAddr(0);
            }else{
                 addressCtrl = OHInterface.getG_server().GetMyCAddr(0,mBundle.getString("gi_type"));
            }

            handler.sendMessage(handler.obtainMessage(mBundle.getInt("address"),addressCtrl));

        }else if (cmd.equals(ADDRESSMESSAGE)){//添加地址信息
            String address = mBundle.getString("address");
            String mobile = mBundle.getString("contacts");
            String contats = mBundle.getString("mobile");
            String gi_type = mBundle.getString("gi_type");
            ICertAddress addressCtrl = OHInterface.getG_server().AddMyCAddr(0, "", address, "", contats, mobile, "",gi_type);
            addressCtrl.gi_text = address;
            addressCtrl.contacts = contats;
            addressCtrl.mobile = mobile;
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("addressmess"),addressCtrl));
        }else if (cmd.equals(REMOVEAD)){//删除地址信息
            ICertAddress result = OHInterface.getG_server().RemoveMyCAddr(mBundle.getInt("addid"), 0);
            if (result!=null)
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("Removeadd"),result));
        } else  if (cmd.equals(GETALLORDER)){//查看全部订单
            IOrderCtrl resorder = OHInterface.getG_server().GetMyOrderListN("'temp','new','confirm','completed','revoke','comment'", "", "", 0, mBundle.getInt("num"));
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("getorder"),resorder));
        }else if (cmd.equals(GETDQRORDER)){//查看待确认订单
            IOrderCtrl resorder = OHInterface.getG_server().GetMyOrderListN("'temp','new'", "", "", 0, mBundle.getInt("num"));
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("getorder"),resorder));
        }else if (cmd.equals(GETZYZORDER)){//查看待付款
            IOrderCtrl resorder =OHInterface.getG_server().GetMyOrderListN("'completed'", "", "", 0, mBundle.getInt("num"));
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("getorder"),resorder));
        }else if (cmd.equals(GETDPJORDER)){//查看带评价订单
            IOrderCtrl resorder = OHInterface.getG_server().GetMyOrderListN("'payed'", "", "", 0, mBundle.getInt("num"));;
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("getorder"),resorder));
        }else if (cmd.equals(ORDERITEM)){//获取每一天的详情
            IorderEntity orderitem = OHInterface.getG_server().LoadOrderInfo(Integer.parseInt(mBundle.getString("orderid")));
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("getorder"),orderitem));
        }else if (cmd.equals(ORDERINPUT)){//获取到图片
            InputStream orderinput = OHInterface.getG_server().InputStreamMedia(Integer.parseInt(mBundle.getString("orderid")), "iorder", m);
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("getorder"),orderinput));
        }else if (cmd.equals(ORDERTEMP)){//取消订单类型
            IOrder orderitem =OHInterface.getG_server().RevokeOrder(Integer.parseInt(mBundle.getString("orderid")));
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("getorder"),orderitem));
        }else if (cmd.equals(RATING)){//评价
            IOrder rating = OHInterface.getG_server().CommentOrder(Integer.parseInt(mBundle.getString("orid")), mBundle.getString("str"), mBundle.getInt("a"), mBundle.getInt("b"), mBundle.getInt("c"));
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("getorder"),rating));

        }else  if (cmd.equals(REPAIS)){//报修
            final IOrder id = OHInterface.getG_server().CreateOrder(0, mBundle.getInt("show"), mBundle.getString("edit"), "service", "pub_ent_guard", true, "");
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("repais"),id));
        }else if(cmd.equals(REPAISCG)){//报修群组
          //  int seller_id, int caddr_id, String item_desc, String type, String goods_code, boolean issave, String ordertime ,int cg_id ,String gi_type,int gc_id   //mBundle.getString("fuleixing")  替换null
            final IOrder id = OHInterface.getG_server().CreateOrder(0, mBundle.getInt("show"), mBundle.getString("edit"), "service",null, true, "", mBundle.getInt("cg_id"),"t",mBundle.getInt("gc_id"));
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("repais"),id));
        }else if (cmd.equals(MEDIAIPC)){//图片上传
            Media ipc = OHInterface.getG_server().UploadMedia(mBundle.getInt("id"), "iorder", m);
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("mehand"),ipc));
        }else  if (cmd.equals(BAOXIU)){
//            IJOrderCtrl baoxiu = OHInterface.getG_server().LoadOrderInfo(Integer.parseInt(mBundle.getString("baoxiu")), 0, 0);
//            handler.sendMessage(handler.obtainMessage(mBundle.getInt("baohand"),baoxiu));
        }else  if (cmd.equals(YOUHUIQUAN)){
            IExChangeCtrl yhq = OHInterface.getG_server().getAvailableVoucherList(Integer.parseInt(mBundle.getString("orcode")));//订单支付优惠券
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("yhq"),yhq));

        }else if (cmd.equals(YOUHUIQUANDUIHUA)){
            IExChangeCtrl yhq = OHInterface.getG_server().getAvailableVoucherList(Integer.parseInt(mBundle.getString("orcode")));//对话订单支付优惠券
            handler.sendMessage(handler.obtainMessage(mBundle.getInt("yhq"),yhq));
        }else if(ADDRESSCENGJI.equals(cmd)){
            //获取小区地址层级
            IGeoinfoCtrl info;
                info = OHInterface.getG_server().GetGeoinfo(mBundle.getInt("gi_id"),mBundle.getInt("prev_id"),mBundle.getString("prev_code"),mBundle.getString("type"),mBundle.getString("gi_type"));
                handler.sendMessage(handler.obtainMessage(ADDRESSCENGJIERROR,info));
        }else if(DSYFIX.equals(cmd)){
            //dsy报修
            final IOrder id = OHInterface.getG_server().CreateOrder(0, mBundle.getInt("show"), mBundle.getString("edit"), "service",mBundle.getString("goods_code") , true, "",0,mBundle.getString("gi_type"));
            handler.sendMessage(handler.obtainMessage(DSYFIXERROR,id));

            //这是分类的二级
        }else if(FIXCATE.equals(cmd)){
//             GoodsEntity id = OHInterface.getG_server().getGoodsList(mBundle.getString("prev_code"),mBundle.getString("gi_type"));
            GoodsClassEntity id1 = OHInterface.getG_server().getGoodsListByPrevCode(mBundle.getString("prev_code"));
            handler.sendMessage(handler.obtainMessage(FIXCATEERROR,id1));

        }
            }
        }).start();
    }

    /**
     * 报修的二级分类
     * @param prev_code
     * @param gi_type
     */
    public void getGoodsList  ( String prev_code,String gi_type){
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, FIXCATE);
        this.mBundle.putString("prev_code",prev_code);
        this.mBundle.putString("gi_type",gi_type);
        RunContext();

    }


    //获取地址层级    gi_type 等于""  就代表是获取地区  否则就是获取下一级
    public void GetGeoinfo(int gi_id, int prev_id, String prev_code, String type ,String gi_type){
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, ADDRESSCENGJI);
        this.mBundle.putInt("gi_id",gi_id);
        this.mBundle.putInt("prev_id",prev_id);
        this.mBundle.putString("prev_code",prev_code);
        this.mBundle.putString("type",type);
        this.mBundle.putString("gi_type",gi_type);

        RunContext();

    }



    public void AddressMessage(int Address,String  gi_type){//查看已填加的地址
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, ADDRESS);
        this.mBundle.putInt("address",Address);
        if(gi_type!=null) {
            this.mBundle.putString("gi_type", gi_type);
        }
        RunContext();

    }

    public void setMediaipc(int id,Media m,int mehand){//上传图片

        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, MEDIAIPC);
         this.m=m;
        this.mBundle.putInt("id",id);
        this.mBundle.putInt("mehand",mehand);
        RunContext();



    }





    public void setRepais(int show,String edit,int repais){
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, REPAIS);
        this.mBundle.putString("edit", edit);
        this.mBundle.putInt("show",show);
        this.mBundle.putInt("repais",repais);

        RunContext();


    }

    public void setRepaiscg(int show,String fuleixing,String edit,int cg_id,int repais,int gc_id){
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, REPAISCG);
        this.mBundle.putString("edit", edit);
        this.mBundle.putString("fuleixing", fuleixing);
        this.mBundle.putInt("show",show);
        this.mBundle.putInt("repais",repais);
        this.mBundle.putInt("cg_id",cg_id);
        this.mBundle.putInt("gc_id",gc_id);

        RunContext();


    }
    //dsy 保修调用
    public void dsyBaoXiu(int show,String edit,int repais,String gi_type,String goods_code){
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, DSYFIX);
        this.mBundle.putString("edit", edit);
        this.mBundle.putInt("show",show);
        this.mBundle.putInt("repais",repais);
        this.mBundle.putString("gi_type", gi_type);
        this.mBundle.putString("goods_code", goods_code);
        RunContext();
    }



    public void AddAddressList(String address, String contacts, String mobile,int addressmess,String gi_type) { //没有地址进行添加
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, ADDRESSMESSAGE);
        this.mBundle.putString("address", address);
        this.mBundle.putString("contacts", contacts);
        this.mBundle.putString("mobile", mobile);
        this.mBundle.putInt("addressmess",addressmess);
        this.mBundle.putString("gi_type", gi_type);
        RunContext();
    }
    public void RemoveAddress(int  addid,int Removeadd){//删除每一天地址
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, REMOVEAD);
        this.mBundle.putInt("addid",addid);
        this.mBundle.putInt("Removeadd",Removeadd);
        RunContext();
    }
public void getAllOrder(int  num,int getorder){//获取全部订单
    this.mBundle = new Bundle();
    this.mBundle.putString(cmd_key, GETALLORDER);
    this.mBundle.putInt("num",num);
    this.mBundle.putInt("getorder",getorder);
    RunContext();
}

    public void getDqrOrder(int  num,int getorder){//获取待确认订单
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, GETDQRORDER);
        this.mBundle.putInt("num",num);
        this.mBundle.putInt("getorder",getorder);
        RunContext();
    }

    public void getZyzOrder(int  num,int getorder){//获取作业中订单
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, GETZYZORDER);
        this.mBundle.putInt("num",num);
        this.mBundle.putInt("getorder",getorder);
        RunContext();
    }

    public void getdPJOrder(int  num,int getorder){//获取作业中订单
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, GETDPJORDER);
        this.mBundle.putInt("num",num);
        this.mBundle.putInt("getorder",getorder);
        RunContext();
    }

public void getOrderitem(String  orderid,int getorder){//获取点击那一条
    this.mBundle = new Bundle();
    this.mBundle.putString(cmd_key, ORDERITEM);
    this.mBundle.putString("orderid", orderid);
    this.mBundle.putInt("getorder",getorder);
    RunContext();

}
    public void getOrderinput(String  orderid, Media me, int getorder){//详情订单
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, ORDERINPUT);
        this.m=me;
        this.mBundle.putString("orderid", orderid);
        this.mBundle.putInt("getorder",getorder);

        RunContext();

    }

    public void getOrderCancel(String  orderid, String state, int getorder){//取消订单
        this.mBundle = new Bundle();
            this.mBundle.putString(cmd_key, ORDERTEMP);
            this.mBundle.putString("orderid", orderid);
            this.mBundle.putInt("getorder", getorder);
        RunContext();

    }
 public void getRating(String orid,String str,int a,int b,int c,int getorder){//评价星级

     this.mBundle = new Bundle();
     this.mBundle.putString(cmd_key, RATING);
     this.mBundle.putString("orid", orid);
     this.mBundle.putString("str", str);
     this.mBundle.putInt("a", a);
     this.mBundle.putInt("b", b);
     this.mBundle.putInt("c", c);
     this.mBundle.putInt("getorder", getorder);
     RunContext();


}

    public void getBaoXiuXinxi(String baoxiu,int baohand){
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, BAOXIU);
        this.mBundle.putString("baoxiu", baoxiu);
        this.mBundle.putInt("baohand", baohand);
        RunContext();


    }

public void getYouHuiQuan(String orcode,int yhq){//订单支付优惠券
    this.mBundle = new Bundle();
    this.mBundle.putString(cmd_key, YOUHUIQUAN);
    this.mBundle.putString("orcode", orcode);
    this.mBundle.putInt("yhq", yhq);
    RunContext();

}


    public void getYouHuiQuanduihua(String orcode,int yhq){//对话订单支付优惠券
        this.mBundle = new Bundle();
        this.mBundle.putString(cmd_key, YOUHUIQUANDUIHUA);
        this.mBundle.putString("orcode", orcode);
        this.mBundle.putInt("yhq", yhq);
        RunContext();

    }



}
